package com.qamaster;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Egg_calc implements Runnable {

	String aniname;
	public static BufferedReader br;
	boolean succ = false;
	int total;
	ArrayList<String> anilist = new ArrayList<String>();
	int per;
	boolean stopped = false;
	Process ps;
	public Egg_calc() {
	}

	public void dostop() {

		try {
			stopped = true;
			ps.destroy();
			br.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void run() {
		String log;
		String[] ss;
		anilist.add("Total");
		Egg_dialog.dtm.setRowCount(Egg_dialog.dtm.getRowCount() + 1);
		Egg_dialog.dtm.setValueAt("Total", Egg_dialog.dtm.getRowCount() - 1, 0);
		Egg_dialog.dtm.setValueAt(0, Egg_dialog.dtm.getRowCount() - 1, 1);
		Egg_dialog.dtm.setValueAt(0.0, Egg_dialog.dtm.getRowCount() - 1, 2);

		String format = ("#.##");
		java.text.DecimalFormat df = new java.text.DecimalFormat(format);
		// adb tag:w *:S
		try {
			Thread.sleep(3000);

			// ps.destroy();
			Runtime.getRuntime().exec("adb logcat -c");
			ps = Runtime.getRuntime().exec("adb logcat NETWORK:D *:S");
			br = new BufferedReader(new InputStreamReader(ps.getInputStream()),
					20000);

			while (!stopped
					&& ((log = br.readLine()) != null)) {
				if (succ && log.contains("AnimalName")) {
					ss = log.split(": ");
					aniname = ss[2].substring(1, ss[2].length() - 2);
					succ = false;

					if (anilist.contains(aniname)) {
						for (int i = 1; i < anilist.size(); i++) {
							if (anilist.get(i).equals(aniname)) {
								Egg_dialog.dtm.setValueAt(Integer
										.parseInt(Egg_dialog.dtm.getValueAt(i,
												1).toString()) + 1, i, 1);
								Egg_dialog.dtm.setValueAt(Integer
										.parseInt(Egg_dialog.dtm.getValueAt(0,
												1).toString()) + 1, 0, 1);
							}
						}
						
					} else {

						anilist.add(aniname);
						Egg_dialog.dtm
								.setRowCount(Egg_dialog.dtm.getRowCount() + 1);
						Egg_dialog.dtm.setValueAt(aniname,
								Egg_dialog.dtm.getRowCount() - 1, 0);
						Egg_dialog.dtm.setValueAt(1,
								Egg_dialog.dtm.getRowCount() - 1, 1);
						Egg_dialog.dtm.setValueAt(0.0,
								Egg_dialog.dtm.getRowCount() - 1, 2);

						Egg_dialog.dtm.setValueAt(Integer
								.parseInt(Egg_dialog.dtm.getValueAt(0, 1)
										.toString()) + 1, 0, 1);

					}
					for (int i = 0; i < anilist.size(); i++) {
						per = Integer.parseInt(Egg_dialog.dtm.getValueAt(i, 1)
								.toString());
						Egg_dialog.dtm.setValueAt(
								df.format(100.00
										* per
										/ Integer.parseInt(Egg_dialog.dtm
												.getValueAt(0, 1).toString())), i,
								2);
					}
				}
				if (log.contains("MealAnimal.php")) {
					succ = true;
				}

				

			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}